package com.admin.commodity.controller;
import com.admin.commodity.pojo.MobileEndDetails;
import com.admin.commodity.service.MobileEndService;
import com.admin.utils.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
/**
 * <p>
 * 商品详细分类 前端控制器
 * </p>
 *
 * @author 零
 * @since 2022-12-13
 */
@RestController
@RequestMapping("/admin/details")
public class MobileEndDetailsController {
    @Autowired
    private MobileEndService detailsService;

    /**
     * 展示一级商品某一类的所有商品
     * @param pid 一级商品id
     * @return R
     */
    @GetMapping("plist/{pid}")
    public R getlist(@PathVariable("pid") Long pid){
        Map<String, Object> details = detailsService.findDetails(pid);
        if (details.size()>0){
            return R.ok().data("data",details);
        }
        return R.error();
    }

    /**
     * 直接通过三级商品id查询商品信息
     * @param id 三级商品id
     * @return R
     */
    @GetMapping("{id}")
    public R getById(@PathVariable("id") Long id){
        MobileEndDetails details = detailsService.getById(id);
        if (details!=null){
            return R.ok().data("items",details);
        }
        return R.error();
    }

    /**
     * 通过二级商品id获取，二级商品下的所有三级商品信息
     * @param cid 二级商品id
     * @return R
     */
    @GetMapping("clist/{cid}")
    public R getByCId(@PathVariable("cid") Long cid){
        QueryWrapper<MobileEndDetails> wrapper = new QueryWrapper<>();
        wrapper.eq("Mobile_end_CommodityId",cid);
        List<MobileEndDetails> list = detailsService.list(wrapper);
        if (list.size()>0){
            return R.ok().data("items",list);
        }
        return R.error();
    }

}

